package easy.id_0_100;

import java.util.ArrayList;
import java.util.Collections;

public class lee67 {
    public static void main(String[] args) {
        String s1="1010";
        String s2="1011";
        System.out.println(addBinary(s1, s2));

    }
    public static String addBinary(String a, String b) {
        int c=0;
        ArrayList<Character> list=new ArrayList<>();
        for(int i=0;i<a.length();i++){
            if(i<b.length()){
                int indexa=a.length()-i-1;
                int indexb=b.length()-i-1;
                int numa=a.charAt(indexa) - '0';
                int numb=b.charAt(indexb)-'0';
                int sum=numa+numb+c;
                c=sum/2;
                int s=sum%2;
                list.add((char)(s+'0'));
            }else{
                int indexa=a.length()-i-1;
                int numa=a.charAt(indexa) - '0';
                int sum=numa+c;
                c=sum/2;
                int s=sum%2;
                list.add((char)(s+'0'));
            }
        }
        if(c==1){
            list.add('1');
        }
        Collections.reverse(list);
        StringBuilder sb = new StringBuilder();
        for (char item : list) {
            sb.append(item);
        }



        return sb.toString();
    }
}
